home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Python 1.3.3 / pbmplus / pbm / libpbm5.c < prev    next >
Text File  |  1996-02-28  |  14KB  |  352 lines

  1. /* libpbm5.c - pbm utility library part 5
  2. **
  3. ** Font routines.
  4. **
  5. ** Copyright (C) 1991 by Jef Poskanzer.
  6. **
  7. ** Permission to use, copy, modify, and distribute this software and its
  8. ** documentation for any purpose and without fee is hereby granted, provided
  9. ** that the above copyright notice appear in all copies and that both that
  10. ** copyright notice and this permission notice appear in supporting
  11. ** documentation.  This software is provided "as is" without express or
  12. ** implied warranty.
  13. */
  14.  
  15. #include "pbm.h"
  16. #include "pbmfont.h"
  17.  
  18. /* The default font, packed in hex so this source file doesn't get huge.
  19. ** You can replace this with your own font using pbm_dumpfont().
  20. */
  21. #define DEFAULTFONT_ROWS 155
  22. #define DEFAULTFONT_COLS 112
  23. static unsigned long defaultfont_bits[DEFAULTFONT_ROWS][(DEFAULTFONT_COLS+31)/32] = {
  24.     {0x00000000L,0x20000c00L,0x10000000L,0x00000000L},
  25.     {0xc600a000L,0x42000810L,0x00000002L,0x00000063L},
  26.     {0x6c00a000L,0x45000810L,0x00000002L,0x00000036L},
  27.     {0x6c00a000L,0x88800808L,0xf2e1dee2L,0x00000036L},
  28.     {0x54000000L,0x80000800L,0x11122442L,0x0000002aL},
  29.     {0x54000001L,0x00000800L,0x11122442L,0x0000002aL},
  30.     {0x54000001L,0x00000800L,0x11122282L,0x0000002aL},
  31.     {0x44000102L,0x00000800L,0x11122382L,0x00000022L},
  32.     {0xee000102L,0x00000800L,0x11e1e102L,0x00000077L},
  33.     {0x00000204L,0x00000800L,0x11002102L,0x00000000L},
  34.     {0x00000000L,0x00000c00L,0x11002102L,0x00000000L},
  35.     {0x00000000L,0x003f8000L,0xe3807600L,0x00000000L},
  36.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  37.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  38.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  39.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  40.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  41.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  42.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  43.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  44.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  45.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  46.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  47.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  48.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  49.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  50.     {0x02000080L,0x00040000L,0x00120000L,0x00000001L},
  51.     {0x04000082L,0x828e1838L,0x20210100L,0x00000002L},
  52.     {0x04000082L,0x82912448L,0x20210100L,0x00000002L},
  53.     {0x08000082L,0x8fd01940L,0x404087c2L,0x00000004L},
  54.     {0x08000080L,0x050c0622L,0x00408102L,0x00000004L},
  55.     {0x10000080L,0x05061874L,0x0040828fL,0x00008008L},
  56.     {0x10000080L,0x1f912688L,0x00408002L,0x00000008L},
  57.     {0x20000000L,0x0a11098cL,0x00408002L,0x00000010L},
  58.     {0x20000080L,0x0a0e0672L,0x00210000L,0x00000010L},
  59.     {0x40000000L,0x00040000L,0x00210000L,0x00000020L},
  60.     {0x00000000L,0x00000000L,0x00120000L,0x00000000L},
  61.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  62.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  63.     {0x00000000L,0x004e0838L,0x7023e1cfL,0x00008000L},
  64.     {0x00000000L,0x00913844L,0x88620208L,0x00008000L},
  65.     {0x08000000L,0x00910844L,0x08a20401L,0x00000004L},
  66.     {0x10000000L,0x01110844L,0x08a20401L,0x00000008L},
  67.     {0x20000000L,0x01110808L,0x3123c781L,0x00000010L},
  68.     {0x400003e0L,0x02110810L,0x0a202441L,0x00000020L},
  69.     {0x20000000L,0x02110820L,0x0bf02442L,0x00000010L},
  70.     {0x10008000L,0x04110844L,0x88242442L,0x00000008L},
  71.     {0x08008002L,0x040e3e7cL,0x7073c382L,0x00000004L},
  72.     {0x00010000L,0x08000000L,0x00000000L,0x00000000L},
  73.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  74.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  75.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  76.     {0x0000e1c0L,0x00000000L,0x00000000L,0x00000000L},
  77.     {0x00011220L,0x00000000L,0x70e38f87L,0x00000000L},
  78.     {0x20011220L,0x00020020L,0x89108448L,0x00008010L},
  79.     {0x10011220L,0x00040010L,0x09314448L,0x00008008L},
  80.     {0x0800e221L,0x02083e08L,0x11514788L,0x00000004L},
  81.     {0x040111e0L,0x00100004L,0x2153e448L,0x00000002L},
  82.     {0x08011020L,0x00083e08L,0x213a2448L,0x00008004L},
  83.     {0x10011040L,0x02040010L,0x01022448L,0x00008008L},
  84.     {0x2000e381L,0x02020020L,0x20e77f87L,0x00000010L},
  85.     {0x00000000L,0x04000000L,0x00000000L,0x00000000L},
  86.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  87.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  88.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  89.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  90.     {0x3803e7efL,0xc73bbe3dL,0xdb863ce7L,0x0000001cL},
  91.     {0x44011224L,0x48910808L,0x91036648L,0x00008022L},
  92.     {0x4c011285L,0x48910808L,0xa1036648L,0x00008026L},
  93.     {0x54011387L,0x081f0808L,0xc102a548L,0x0000802aL},
  94.     {0x54011285L,0x09910808L,0xe102a548L,0x0000802aL},
  95.     {0x4e011204L,0x08910848L,0x9112a4c8L,0x00008027L},
  96.     {0x40011224L,0x08910848L,0x891224c8L,0x00008020L},
  97.     {0x3803e7efL,0x073bbe31L,0xcff77e47L,0x0000001cL},
  98.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  99.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  100.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  101.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  102.     {0x00000000L,0x00000000L,0x00000003L,0x00000000L},
  103.     {0x0003e1cfL,0x87bff7efL,0xdfbf77c2L,0x00000000L},
  104.     {0x00013224L,0x48a4a244L,0x89122442L,0x00000000L},
  105.     {0x00011224L,0x4824a244L,0xa8a14482L,0x00000000L},
  106.     {0x00013227L,0x8e04226cL,0xa8414102L,0x00000000L},
  107.     {0x0001e224L,0x83842228L,0xa8a08102L,0x00000000L},
  108.     {0x00010224L,0x40842228L,0xd8a08242L,0x00000000L},
  109.     {0x00010224L,0x48843638L,0x51108442L,0x00000000L},
  110.     {0x0003c1ceL,0x6f1f1c10L,0x53b9c7c2L,0x00000000L},
  111.     {0x00000060L,0x00000000L,0x00000002L,0x00000000L},
  112.     {0x00000000L,0x00000000L,0x00000003L,0x00000000L},
  113.     {0xfe000000L,0x00000000L,0x00000000L,0x0000007fL},
  114.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  115.     {0x00010180L,0x000000c0L,0x003001c0L,0x00000000L},
  116.     {0x08008081L,0x00040040L,0x00100200L,0x00000004L},
  117.     {0x10008082L,0x80040040L,0x00100200L,0x00000008L},
  118.     {0x10004084L,0x40023c78L,0x70f1c7c7L,0x00004008L},
  119.     {0x10004080L,0x00000244L,0x89122208L,0x00008008L},
  120.     {0x20002080L,0x00001e44L,0x8113e208L,0x00008010L},
  121.     {0x10002080L,0x00002244L,0x81120208L,0x00008008L},
  122.     {0x10001080L,0x00002244L,0x89122208L,0x00008008L},
  123.     {0x10001080L,0x00001db8L,0x70e9c787L,0x00008008L},
  124.     {0x10000880L,0x00000000L,0x00000000L,0x00008008L},
  125.     {0x08000180L,0x00000000L,0x00000000L,0x00008004L},
  126.     {0x00000000L,0x1fc00000L,0x00000007L,0x00000000L},
  127.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  128.     {0x00030080L,0x981c0000L,0x00000000L,0x00000000L},
  129.     {0x20010000L,0x08040000L,0x00000000L,0x00000010L},
  130.     {0x10010000L,0x08040000L,0x00000000L,0x00000008L},
  131.     {0x10016387L,0x898474b8L,0x72e1d5c7L,0x00000008L},
  132.     {0x10019080L,0x8a042a64L,0x89122208L,0x00008008L},
  133.     {0x08011080L,0x8c042a44L,0x89122207L,0x00000004L},
  134.     {0x10011080L,0x8a042a44L,0x89122200L,0x00008008L},
  135.     {0x10011080L,0x89042a44L,0x89122208L,0x00008008L},
  136.     {0x1003bbe0L,0x98dfebe6L,0x71e1e787L,0x00000008L},
  137.     {0x10000000L,0x80000000L,0x01002000L,0x00000008L},
  138.     {0x20000000L,0x80000000L,0x01002000L,0x00000010L},
  139.     {0x00000007L,0x00000000L,0x03807000L,0x00000000L},
  140.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  141.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  142.     {0x00008000L,0x00000000L,0x10410000L,0x00000000L},
  143.     {0x00008000L,0x00000000L,0x20408000L,0x00000000L},
  144.     {0x0001f66eL,0xfdfbf77cL,0x20408000L,0x00000000L},
  145.     {0x24008224L,0x488a2248L,0x20408240L,0x00000012L},
  146.     {0x54008224L,0x4a842210L,0x40404540L,0x0000002aL},
  147.     {0x48008222L,0x8a8a1420L,0x20408480L,0x00000024L},
  148.     {0x00008a23L,0x85111c44L,0x20408000L,0x00000000L},
  149.     {0x000071d1L,0x0531887cL,0x20408000L,0x00000000L},
  150.     {0x00000000L,0x00000800L,0x20408000L,0x00000000L},
  151.     {0x00000000L,0x00000800L,0x10410000L,0x00000000L},
  152.     {0x00000000L,0x00003000L,0x00000000L,0x00000000L},
  153.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  154.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  155.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  156.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  157.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  158.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  159.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  160.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  161.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  162.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  163.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  164.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  165.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  166.     {0x00000000L,0x00000000L,0x00000000L,0x00000000L},
  167.     {0x00000000L,0x20000c00L,0x10000000L,0x00000000L},
  168.     {0xc600a000L,0x42000810L,0x00000002L,0x00000063L},
  169.     {0x6c00a000L,0x45000810L,0x00000002L,0x00000036L},
  170.     {0x6c00a000L,0x88800808L,0xf2e1dee2L,0x00000036L},
  171.     {0x54000000L,0x80000800L,0x11122442L,0x0000002aL},
  172.     {0x54000001L,0x00000800L,0x11122442L,0x0000002aL},
  173.     {0x54000001L,0x00000800L,0x11122282L,0x0000002aL},
  174.     {0x44000102L,0x00000800L,0x11122382L,0x00000022L},
  175.     {0xee000102L,0x00000800L,0x11e1e102L,0x00000077L},
  176.     {0x00000204L,0x00000800L,0x11002102L,0x00000000L},
  177.     {0x00000000L,0x00000c00L,0x11002102L,0x00000000L},
  178.     {0x00000000L,0x003f8000L,0xe3807600L,0x00000000L}
  179.     };
  180.  
  181. bit**
  182. pbm_defaultfont( fcolsP, frowsP )
  183.     int* fcolsP;
  184.     int* frowsP;
  185.     {
  186.     bit** defaultfont;
  187.     int row, col, scol;
  188.     unsigned long l;
  189.  
  190.     defaultfont = pbm_allocarray( DEFAULTFONT_COLS, DEFAULTFONT_ROWS );
  191.     for ( row = 0; row < DEFAULTFONT_ROWS; ++row )
  192.     {
  193.     for ( col = 0; col < DEFAULTFONT_COLS; col += 32 )
  194.         {
  195.         l = defaultfont_bits[row][col / 32];
  196.         for ( scol = min( col + 32, DEFAULTFONT_COLS ) - 1;
  197.           scol >= col; --scol )
  198.         {
  199.         if ( l & 1 )
  200.             defaultfont[row][scol] = 1;
  201.         else
  202.             defaultfont[row][scol] = 0;
  203.         l >>= 1;
  204.         }
  205.         }
  206.     }
  207.  
  208.     *fcolsP = DEFAULTFONT_COLS;
  209.     *frowsP = DEFAULTFONT_ROWS;
  210.     return defaultfont;
  211.     }
  212.  
  213. void
  214. pbm_dissectfont( font, frows, fcols, char_heightP, char_widthP, char_aheightP, char_awidthP, char_row0, char_col0 )
  215.     bit** font;
  216.     int frows;
  217.     int fcols;
  218.     int* char_heightP;
  219.     int* char_widthP;
  220.     int* char_aheightP;
  221.     int* char_awidthP;
  222.     int char_row0[95];
  223.     int char_col0[95];
  224.     {
  225.     /*
  226.     ** This routine expects a font bitmap representing the following text:
  227.     **
  228.     ** (0,0)
  229.     **    M ",/^_[`jpqy| M
  230.     **
  231.     **    /  !"#$%&'()*+ /
  232.     **    < ,-./01234567 <
  233.     **    > 89:;<=>?@ABC >
  234.     **    @ DEFGHIJKLMNO @
  235.     **    _ PQRSTUVWXYZ[ _
  236.     **    { \]^_`abcdefg {
  237.     **    } hijklmnopqrs }
  238.     **    ~ tuvwxyz{|}~  ~
  239.     **
  240.     **    M ",/^_[`jpqy| M
  241.     **
  242.     ** The bitmap must be cropped exactly to the edges.
  243.     **
  244.     ** The dissection works by finding the first blank row and column; that
  245.     ** gives the height and width of the maximum-sized character, which is
  246.     ** not too useful.  But the distance from there to the opposite side is
  247.     ** an integral multiple of the cell size, and that's what we need.  Then
  248.     ** it's just a matter of filling in all the coordinates.
  249.     **
  250.     ** The difference between char_height, char_width and char_aheight,
  251.     ** char_awidth is that the first is the size of the cell including
  252.     ** spacing, while the second is just the actual maximum-size character.
  253.     */
  254.     int brow, bcol, row, col, d, ch;
  255.     bit b;
  256.  
  257.     /* Find first blank row. */
  258.     for ( brow = 0; brow < frows / 6; ++brow )
  259.     {
  260.     b = font[brow][0];
  261.     for ( col = 1; col < fcols; ++col )
  262.         if ( font[brow][col] != b )
  263.         goto nextrow;
  264.     goto gotblankrow;
  265.     nextrow: ;
  266.     }
  267.     pm_error( "couldn't find blank row in font" );
  268.  
  269. gotblankrow:
  270.     /* Find first blank col. */
  271.     for ( bcol = 0; bcol < fcols / 8; ++bcol )
  272.     {
  273.     b = font[0][bcol];
  274.     for ( row = 1; row < frows; ++row )
  275.         if ( font[row][bcol] != b )
  276.         goto nextcol;
  277.     goto gotblankcol;
  278.     nextcol: ;
  279.     }
  280.     pm_error( "couldn't find blank col in font" );
  281.  
  282. gotblankcol:
  283.     /* Now compute character cell size. */
  284.     d = frows - brow;
  285.     *char_heightP = d / 11;
  286.     if ( *char_heightP * 11 != d )
  287.     pm_error( "problem computing character cell height" );
  288.     d = fcols - bcol;
  289.     *char_widthP = d / 15;
  290.     if ( *char_widthP * 15 != d )
  291.     pm_error( "problem computing character cell width" );
  292.     *char_aheightP = brow;
  293.     *char_awidthP = bcol;
  294.  
  295.     /* Now fill in the 0,0 coords. */
  296.     row = *char_heightP * 2;
  297.     col = *char_widthP * 2;
  298.     for ( ch = 0; ch < 95; ++ch )
  299.     {
  300.     char_row0[ch] = row;
  301.     char_col0[ch] = col;
  302.     col += *char_widthP;
  303.     if ( col >= *char_widthP * 14 )
  304.         {
  305.         col = *char_widthP * 2;
  306.         row += *char_heightP;
  307.         }
  308.     }
  309.     }
  310.  
  311. void
  312. pbm_dumpfont( font, fcols, frows )
  313.     bit** font;
  314.     int fcols;
  315.     int frows;
  316.     {
  317.     /* Dump out font as C source code. */
  318.     int row, col, scol, lperrow;
  319.     unsigned long l;
  320.  
  321.     printf( "#define DEFAULTFONT_ROWS %d\n", frows );
  322.     printf( "#define DEFAULTFONT_COLS %d\n", fcols );
  323.     printf( "static unsigned long defaultfont_bits[DEFAULTFONT_ROWS][(DEFAULTFONT_COLS+31)/32] = {\n" );
  324.     for ( row = 0; row < frows; ++row )
  325.     {
  326.     lperrow = 0;
  327.     for ( col = 0; col < fcols; col += 32 )
  328.         {
  329.         if ( lperrow == 0 )
  330.         printf( "    {" );
  331.         else if ( lperrow % 6 == 0 )
  332.         {
  333.         printf( ",\n     " );
  334.         lperrow = 0;
  335.         }
  336.         else
  337.         printf( "," );
  338.         l = 0;
  339.         for ( scol = col; scol < min( col + 32, fcols ); ++scol )
  340.         {
  341.         l <<= 1;
  342.         if ( font[row][scol] )
  343.             l |= 1;
  344.         }
  345.         printf( "0x%08lxL", l );
  346.         ++lperrow;
  347.         }
  348.     printf( "}%s\n", row == frows - 1 ? "" : "," );
  349.     }
  350.     printf( "    };\n" );
  351.     }
  352.